const WXAPI = require('apifm-wxapi')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: undefined,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let recharge_amount_min = wx.getStorageSync('recharge_amount_min')
    if (!recharge_amount_min) {
      recharge_amount_min = 0;
    }
    this.setData({
      uid: wx.getStorageSync('uid'),
      recharge_amount_min: recharge_amount_min
    });
    console.log("获取水果类型------")
    this.getFruitType();
  },

  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  bindCancel: function () {
    wx.navigateBack({})
  },

  // 申请入库
  async bindSave(e) {
    var that = this;
    var number = e.detail.value.number;
    var type = e.detail.value.type;

    if (number == "" || number * 1 <= 0) {
      wx.showModal({
        title: '错误',
        content: '请填写正确的入库数量',
        showCancel: false
      })
      return
    }
    if (number * 1 < 1 * 1) {
      wx.showModal({
        title: '错误',
        content: '单次入库水果至少' + 1 + '千克',
        showCancel: false
      })
      return
    }
    var stockOrderInfo = {
      'stockOrderNumber':number,
      'fruitType':type,
      'userId':wx.getStorageSync('userid'),
      'stokcInOut': 0
    }
    console.log("入库信息："+JSON.stringify(stockOrderInfo))
    const res = await WXAPI.addStockRequest(stockOrderInfo);
    console.log("入库结果："+JSON.stringify(res))
    if(res.code == 20000){
      wx.showModal({
        title: '成功',
        content: '入库申请提交成功，申请入库['+type+']'+number + '千克',
        showCancel: false
      })
    }else{
      wx.showModal({
        title: '入库申请提交失败',
        content: res.msg,
        showCancel: false
      })
    }
    
  },

  async getFruitType(){
    var that = this;
  
    const fruitTypeRes = await WXAPI.fruitTypeList()
    console.log("获取水果类型:"+JSON.stringify(fruitTypeRes))
    if (fruitTypeRes.code == 20000) {
      console.log("缓存水果类型"+JSON.stringify(fruitTypeRes.data))
      that.setData({
        fruitType: fruitTypeRes.data
      })
    }
  }


})